package com.ztmg.common.util.handleSign;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.security.PublicKey;
import java.security.Signature;

import javax.crypto.Cipher;


public class Main {
	static String privateKeyUp = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL9u/fhunnIFicQQd/dFKWtM+8tO+g/XQtBBzQI5h/3BqGTwvjQJaxqq+6dKxxLSR5QdU+fipiR9Nm7WbLkq9Aya3u2CT3X3bgBqT4LtKxXon5WE8RwtedMow1XD+T/OBvL4lcal5H0KHGvnKrxtzYHWhroYBMnD0Mnp6Ix6taQhAgMBAAECgYEArB0ztKhoRqXaDnCsG6KQMcv+WvW/+HwWsz1mAAkPzsPODisxJ4IMTObL6cZeo/pxpqaTqNnrfMUjKpYZHIaU8itbFqDF+6NdXUPgzppek6i+XtolaZJLsujaozcDXOXUmJeQAZmc/bTyym2mFQcE2tmnES99Tcxb1osM4FQxcpECQQD5q5KFualCVijI8asgIiBLxKHNXrnX9GustVB/9OfA8bb8HoC/ORHux1v+SJCCQvmthtTnw5DUYkHkyXG5KVLFAkEAxElys0wBAKI0KEj9qwvGToaAncSMeEOd2ea38gbJOWD+mq/YFTluQKev52vk5cj02ALilWjB61hTg+5xWD0xrQJAe6zR2AwwG/wyF9gevLwq4110CRdzV9ZrwBHQ3pGie6FA04MIo7DRgUaf+0ZWzy/+Hkc8D9KAW7SQj5R6EQ63QQJARypeBLWss18by/NSzT4SowvO++R0DXJue79Tt9NQB5asGMdib6eH2rcxZpVOwyUN6Hd7UTJPBJXcibvcqmJdbQJAA5iptviyGUQAou9tYSyL1/DB559u0CIBe/IEH7sgQZlEUNXdFdRlDN+CkMFzKc2VQdu9HQc5yXcatMHTuGy74g==";//支付平台分配私钥文件
	static String deskey = "MmE1MGZmMjZkMjA5NDU4YmI3MmU3ODVi";//支付平台分配3des加密密钥
	static String publicKey = "MmE1MGZmMjZkMjA5NDU4YmI3MmU3ODVi";//支付平台分配3des加密密钥
	static String KEY_TYPE = "SHA1withRSA";
	static SignUtilsImpl SignUtils = new SignUtilsImpl();
	
	public static void main(String[] args)throws Exception {
		// TODO Auto-generated method stub
//
//          String text = "tranamt=1&orderid=M2018061913340035000011&account=197912&sno=2160805026&toaccount=1001117&thirdsystem=xjtuweiwuye&thirdorderid=2018061913334514905&state=1&orderdesc=水票购买&praram1=order_water_tick";
//		  String des3Encode=SignUtils.encrypt(text,deskey,"DESede");
//
//  		String sign = SignUtils.sign(des3Encode, privateKeyUp, KEY_TYPE);
		String des3Encode = "account=197912&orderdesc=订水&orderid=M2018070616453186197912000002&praram1=order_water&sno=2160805026&state=1&thirdorderid=2018070616453526433&thirdsystem=xjtuweiwuye&toaccount=1001117&tranamt=4";
		des3Encode = SignUtils.encrypt(des3Encode,deskey,"DESede");
		String sign = "JcTxJFTQ4Jv1dLTf8ziwnZocKhtRvnvXNnwJCTI6Ffoa5taCsIs62rxCr5xi7vk0u6fqLXpNN1JwcbZP+bKhE0o\\/PlmAdmjfUYRXKZdidMyVnwOfX7Qjh\\/CYvw79tj9kWBj7j7MumciI7Io1AEllymAPdqR5wSr1gAXf7+FyRFg=";
//		boolean flag = SignUtils.verify(des3Encode,sign,privateKeyUp,KEY_TYPE);

//		String  = SignUtils.decrypt(sign,deskey,"DESede");
//		String sign = "OfM6VYSyu3ThJB0Rw5btYnZVHLO2xdO3kCbuU\\/djf1Jk0OPRbygBwAUHCuvDMKFAVrmg326IPnc\\/HOl\\/zdgyWSmWyAmojx\\/pAYlHd9E6ilx0zbgHSIrysrczLNdRkPmprfp1EjnWuMqBNIPVXYKW8fQ33QArTkAMT\\/G4xHOAnis=";

//		System.out.println(text1);
		Boolean flag = SignatureUtil.verify(Base64.decode(des3Encode),Base64.decode(sign),Base64.decode(privateKeyUp),KEY_TYPE);
		System.out.println(flag);
	}
	
	public static String encrypt(String text, String key, String algorithm) {
		   try {
		      byte[] bytes = text.getBytes("UTF-8"); // 待加/解密的数据
		      byte[] keyData = Base64.decode(key); //密钥数据
		      byte[] cipherBytes = SymmtricCryptoUtil.symmtricCrypto(bytes,
		            keyData, algorithm, Cipher.ENCRYPT_MODE);
		      return Base64.encode(cipherBytes);
		   } catch (GeneralSecurityException e) {
		      return "";
		   } catch (UnsupportedEncodingException e1) {
		      return "";
		   }
		}
	/**
	 * 密码加密时，填充字符串为8的倍数。<p>
	 * （此方法在模式是CBC模式，填充方式为NoPadding方式的情况下，用字节零填充.）
	 *
	 * @param str
	 *          密码
	 * @return 加密后的密文
	 */
	public static byte [] FormateData(String str) throws UnsupportedEncodingException {

		int yu = str.length() % 8;
		if(yu != 0){
			int size = 8 - yu;
			byte [] arr = new byte [str.length() + size];
			byte [] data = str.getBytes("UTF-8");
			int i = 0;
			for (; i < data.length; i++) {
				arr[i] = data[i];
			}
			for (int j = 0; j < size; j++,i++) {
				arr[i] = new byte [] {0}[0];
			}
			return arr;
		}
		return str.getBytes("UTF-8");
	}
	public static byte[] convertHexString(String ss)
	{
		byte digest[] = new byte[ss.length() / 2];
		for(int i = 0; i < digest.length; i++)
		{
			String byteString = ss.substring(2 * i, 2 * i + 2);
			int byteValue = Integer.parseInt(byteString, 16);
			digest[i] = (byte)byteValue;
		}

		return digest;
	}
	private static String removeAndStr(String str) {
		String sendBuf = str;

		try {
			sendBuf = URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return sendBuf;
	}



}
